/*
Copyright 2020 Bonitasoft S.A.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// IMPORTANT: this script is run by npm when calling 'npm version'
// We are not installing node dependencies when running the GitHub workflow '.github/workflows/release.yml'
// So please do not import code that is not provided by the node runtime. Otherwise, update the GitHub workflow definition
import { readFileSync, writeFileSync } from 'node:fs';

if (process.env.IS_RELEASING) {
  updateVersionInFilesOnRelease();
} else {
  manageVersionSuffixInFiles();
}

function updateVersionInFilesOnRelease() {
  log('Updating version in files for release');
  const currentVersion = getCurrentVersion();
  log('Current version', currentVersion);
  updateVersionInSourceFile(currentVersion);
  log('Files have been updated');
}

function manageVersionSuffixInFiles() {
  log('Managing version suffix in files');
  const currentVersion = getCurrentVersion();
  log('Current version', currentVersion);

  const newVersion = addOrRemoveVersionSuffix(currentVersion);
  log('New version', newVersion);

  updateVersionInNpmFile('./package.json', newVersion);
  updateVersionInNpmFile('./package-lock.json', newVersion);
  updateVersionInSonarFile(newVersion);
  updateVersionInSourceFile(newVersion);

  log('Files have been updated');
}

function log(...data) {
  // eslint-disable-next-line no-console
  console.info(...data);
}

function readFileContent(path) {
  return readFileSync(path, 'utf8').toString();
}

function getCurrentVersion() {
  const json = readFileContent('./package.json');
  const packageJson = JSON.parse(json);
  return packageJson.version;
}

function addOrRemoveVersionSuffix(version) {
  return version.endsWith('-post') ? version.replace(/-post$/, '') : `${version}-post`;
}

function updateVersionInNpmFile(path, newVersion) {
  const json = readFileContent(path);
  const packageJson = JSON.parse(json);
  packageJson.version = newVersion;
  writeFileSync(path, JSON.stringify(packageJson, null, 2) + '\n');
}

function updateVersionInSonarFile(newVersion) {
  const path = './sonar-project.properties';
  const content = readFileContent(path);
  // replace the 1st occurrence, is ok as a key appears only once in the file
  const updatedContent = content.replace(/sonar\.projectVersion=.*/, `sonar.projectVersion=${newVersion}`);
  writeFileSync(path, updatedContent);
}

function updateVersionInSourceFile(newVersion) {
  const path = 'src/component/version.ts';
  const content = readFileContent(path);
  // replace the 1st occurrence, is ok as the constant appears only once in the file
  const updatedContent = content.replace(/const libraryVersion =.*/, `const libraryVersion = '${newVersion}';`);
  writeFileSync(path, updatedContent);
}
